package com.example.smartcitydemo.ui.person

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.DividerItemDecoration
import com.blankj.utilcode.util.BusUtils
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.ModifyPersonInfo
import com.example.smartcitydemo.bean.PersonModifyInfo
import com.example.smartcitydemo.databinding.FragmentPersonSettingBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ext.modifyInfo
import com.example.smartcitydemo.viewModel.person.PersonViewModel
import com.google.android.material.dialog.MaterialAlertDialogBuilder

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.person
 * @NAME: Caleb.F
 * @DATE: 2021/11/11 9:20
 * @Email: 1919796283@qq.com
 */
class PersonSettingFragment :
    BaseFragment<FragmentPersonSettingBinding>(R.layout.fragment_person_setting) {
    private val personViewModel: PersonViewModel by activityViewModels()
    private val personModifyAdapter: PersonModifyAdapter by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
        PersonModifyAdapter().apply {
            init()
            setDiffCallback(PersonModifyInfo.callback)
            setOnItemClickListener { _, view, pos ->
                val tvLeft = view.findViewById<TextView>(R.id.tv_left)
                val tvRight = view.findViewById<TextView>(R.id.tv_right)
                if (1 == pos) {
                    //处理性别数据
                    showModifySex(tvRight.text.trim().toString())
                } else {
                    MaterialAlertDialogBuilder(requireContext()).modifyInfo(
                        requireContext(),
                        "请输入${tvLeft.text.trim().toString()}",
                        tvRight.text.trim().toString()
                    ) {
                        //发请求
                        when (pos) {
                            0 -> personViewModel.modifyPersonInfo(ModifyPersonInfo(nickName = it))
                            2 -> personViewModel.modifyPersonInfo(ModifyPersonInfo(email = it))
                            3 -> personViewModel.modifyPersonInfo(ModifyPersonInfo(idCard = it))
                            4 -> personViewModel.modifyPersonInfo(ModifyPersonInfo(phonenumber = it))
                        }

                    }
                }
            }
        }
    }

    /**
     * 修改性别数据
     * @param toString String
     */
    private fun showModifySex(toString: String) {
        val sexId = if ("男" == toString) 0 else 1
        MaterialAlertDialogBuilder(requireContext()).apply {
            setTitle("性别")
            setSingleChoiceItems(arrayOf("男", "女"), sexId) { dialog, which ->
                Log.d(TAG, "showModifySex: $which")
                personViewModel.modifyPersonInfo(ModifyPersonInfo(sex = "$which"))
                dialog.dismiss()
            }
        }.show()
    }

    override fun initData() {

    }

    override fun initView(bindView: FragmentPersonSettingBinding, savedInstanceState: Bundle?) {
        bindView.run {
            ryModify.adapter = personModifyAdapter
            ryModify.addItemDecoration(
                DividerItemDecoration(
                    ryModify.context,
                    DividerItemDecoration.VERTICAL
                )
            )
        }
    }

    override fun addObservers(bindView: FragmentPersonSettingBinding) {
        personViewModel.personInfo.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                bindView.data = it.data
                val list: MutableList<PersonModifyInfo> = mutableListOf()
                it.data!!.run {
                    list.add(PersonModifyInfo("昵称", nickName!!))
                    list.add(PersonModifyInfo("性别", if (sex == "0") "男" else "女"))
                    list.add(PersonModifyInfo("邮箱", email!!))
                    list.add(PersonModifyInfo("身份证", idCard!!))
                    list.add(PersonModifyInfo("联系电话", phonenumber!!))
                }
                personModifyAdapter.setDiffNewData(list)
            }
        }
        personViewModel.modifyResult.observe(viewLifecycleOwner) {
            if (it is Resource.Success) {
                //成功则再去拉一次数据
                personViewModel.getPersonInfo()
            }
        }
    }
}